layui.define([], function (exports) {
    // 本地localstorage数据处理
    function getStorage(imid) {
        // 历史信息
        var historyEvent = {
            // 获取用户与所有人的历史信息
            getAllHistory: function () {
                return this.getMyLayimMobile()['history'] || {};
            },
            // 获取当前用户与某个人的历史
            getHistoryById: function (id) {
                if (!id) {
                    throw new Error('id 不存在');
                }
                return this.getAllHistory()['friend' + id] || {};
            },
            // 修改用户与某个人历史中的某个字段,对方id,key,value
            updateHistoryById: function (obj) {
                var id = obj.id;
                var key = obj.key;
                var value = obj.value;
                if (id && key && (value !== undefined)) {
                    var myLocal = this.getMyLayimMobile();
                    myLocal['history'] && myLocal['history']['friend' + id] && (myLocal['history']['friend' + id][key] = value);
                    layui.data('layim-mobile', {
                        key: imid,
                        value: myLocal
                    });
                } else {
                    throw new Error('参数不齐全。')
                }
            },
            // 添加或更新用户与某人的历史，对方id,value
            setHistory: function (obj) {
                var id = obj.id;
                var value = obj.value;
                if (id && (value !== undefined)) {
                    var myLocal = this.getMyLayimMobile();
                    myLocal['history']['friend' + id] = value;
                    layui.data('layim-mobile', {
                        key: imid,
                        value: myLocal
                    });
                } else {
                    throw new Error('参数不齐全。')
                }
            },
            // 删除与某人的历史消息，对方id。
            deleteHistory: function(obj){
                var id = obj.id;
                if (id) {
                    var myLocal = this.getMyLayimMobile();
                    delete myLocal['history']['friend' + id];
                    layui.data('layim-mobile', {
                        key: imid,
                        value: myLocal
                    });
                } else {
                    throw new Error('参数不齐全。')
                }
            },
            // 获取自己layim-mobile值
            getMyLayimMobile() {
                return layui.data('layim-mobile')[imid] || {}
            }

        }

        //  聊天信息
        var chatlogEvent = {
            // 获取用户与所有人的聊天信息
            getAllChatlog: function () {
                return this.getMyLayimMobile()['chatlog'] || {};
            },
            // 获取当前用户与某个人的所有聊天消息
            getChatlogById: function (id) {
                if (!id) {
                    throw new Error('id 不存在');
                }
                return this.getAllChatlog()['friend' + id] || [];
            },
            // 修改用户与某个人某条聊天信息中的某个字段,对方id,index,key,value
            updateChatlogById: function (obj) {
                var id = obj.id;
                var key = obj.key;
                var value = obj.value;
                var index = obj.index;
                if (id && key && (value !== undefined) && index) {
                    var myLocal = this.getMyLayimMobile();
                    myLocal['chatlog']['friend' + id][index][key] = value;
                    layui.data('layim-mobile', {
                        key: imid,
                        value: myLocal
                    });
                } else {
                    throw new Error('参数不齐全。')
                }
            },
            // 添加与某人的一条聊天信息。
            addChatlogById: function (obj) {
                var id = obj.id;
                var value = obj.value;
                if (id && (value !== undefined)) {
                    var myLocal = this.getMyLayimMobile();
                    myLocal['chatlog']['friend' + id].push(value);
                    layui.data('layim-mobile', {
                        key: imid,
                        value: myLocal
                    });
                } else {
                    throw new Error('参数不齐全。')
                }
            },
            // 删除与某人的某一条聊天信息,对方id，消息index
            deleteChatlogById: function (obj) {
                var id = obj.id;
                var index = obj.index;
                if (id && (index || index===0)) {
                    var myLocal = this.getMyLayimMobile();
                    myLocal['chatlog']['friend' + id].splice(index, 1);
                    layui.data('layim-mobile', {
                        key: imid,
                        value: myLocal
                    });
                } else {
                    throw new Error('参数不齐全。')
                }
            },
            // 删除与某人的所有聊天信息对方id
            deleteChatlog: function(obj){
                var id = obj.id;
                if (id) {
                    var myLocal = this.getMyLayimMobile();
                    delete myLocal['chatlog']['friend' + id];
                    layui.data('layim-mobile', {
                        key: imid,
                        value: myLocal
                    });
                } else {
                    throw new Error('参数不齐全。')
                }
            },
            // 获取自己layim-mobile值
            getMyLayimMobile() {
                return layui.data('layim-mobile')[imid] || {}
            }
        }

        function __assgin(){
            var obj = {}
            for(var i=0;i<arguments.length;i++){
                __merge(arguments[i]);
            }
            function __merge(source){ 
                for(var key in source){
                    obj[key] = source[key]
                }
            }
            return obj;
        }

        return __assgin(historyEvent,chatlogEvent);
    }
    exports('storage', getStorage);
})